package com.lzh.o2o.web.frontend;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lzh.o2o.dto.ShopExecution;
import com.lzh.o2o.pojo.Area;
import com.lzh.o2o.pojo.Shop;
import com.lzh.o2o.pojo.ShopCategory;
import com.lzh.o2o.service.AreaService;
import com.lzh.o2o.service.ShopCategoryService;
import com.lzh.o2o.service.ShopService;
import com.lzh.o2o.util.HttpServletRequestUtil;

/** 
* @author  haohao: 
* @date 创建时间：2018年9月20日 下午9:02:23 
* @version 1.0 
* @parameter 
* @since  
* @return  
*/
@Controller
@RequestMapping("/frontend")
public class ShopListController {

	@Autowired
	private AreaService areaService;
	@Autowired
	private ShopCategoryService shopCategoryService;
	@Autowired
	private ShopService shopService;
	Logger logger = LoggerFactory.getLogger(ShopListController.class);

	//返回商品列表页里的ShopCategory列表，（二级或者一级目录）,以及区域信息列表
	@RequestMapping(value="/listshopspageinfo",method=RequestMethod.GET)
	@ResponseBody
	private Map<String,Object> listShopPageInfo(HttpServletRequest request){
		logger.info("获取店铺的二级列表一机列表信息与区域信息开始");
		Map<String,Object> modelMap=new HashMap<String,Object>();
		//试着从前端请求中获取parentId
		Long parentId = HttpServletRequestUtil.getLong(request, "parentId");
		List<ShopCategory> shopCategoryList=null;
		if(parentId!=-1){
			
			//如果parentId存在，则取出该一级ShopCategory下的二级ShopCategory列表
			try{
				logger.debug("查询一级店铺列表下的二级店铺列表");
				ShopCategory shopCategoryCondition = new ShopCategory();
				ShopCategory parent = new ShopCategory();
				parent.setShopCategoryId(parentId);
				shopCategoryCondition.setParent(parent);
				//取出二级列表前端展示
				shopCategoryList= shopCategoryService.getShopCategoryList(shopCategoryCondition);
				
			}catch(Exception e){
				logger.error("获取二级店铺列表失败");
				modelMap.put("success", false);
				modelMap.put("errMsg", e.getMessage());
			}
		}else{
			try{
				logger.debug("获取全部店铺列表信息，一级二级");
				//如果parentId不存在，则取出所有一级ShopCategory(用户在首页选择的是全部商店列表)
				shopCategoryList=shopCategoryService.getShopCategoryList(null);
			}catch(Exception e){
				logger.error("获取全部店铺列表信息异常");
				modelMap.put("success", false);
				modelMap.put("errMsg", e.getMessage());
			}
		}
			//将获取的店铺列表信息返回前端用ajax显示
			modelMap.put("shopCategoryList", shopCategoryList);
			List<Area> areaList=null;
			try{
				logger.debug("获取店铺区域列表信息");
				//获取区域列表信息
				areaList= areaService.getAreaList();
				modelMap.put("areaList", areaList);
				modelMap.put("success", true);

			}catch(Exception e){
				logger.error("获取店铺区域列表信息异常");
				modelMap.put("success", false);
				modelMap.put("errMsg", e.getMessage());
			}
		
		logger.info("获取店铺列表信息与区域信息结束");
		return modelMap;
		
	}
	
	/**
	 * 
	* @Title: listShops 
	* @Description: 获取指定列表的店铺列表 
	* @param request
	* @return Map<String,Object>
	* @author haohao
	* @date 2018年9月20日下午9:24:06
	 */
	@RequestMapping(value="/listshops",method=RequestMethod.GET)
	@ResponseBody
	private Map<String,Object>listShops(HttpServletRequest request){
		logger.info("获取指定列表下的店铺列表信息方法开始");
		Map<String,Object> modelMap=new HashMap<String,Object>();
		//获取页码
		int pageIndex = HttpServletRequestUtil.getInt(request, "pageIndex");
		//获取一页需要显示的需要数据条路
		int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
		//非空判断
		if((pageIndex >-1)&&(pageSize>-1)){
			logger.debug("根据传入的页码查询店铺列表信息");
			//试着获取一级类别Id
			long parentId = HttpServletRequestUtil.getLong(request, "parentId");
			//试着获取特定二级类别Id
			long shopCategoryId=HttpServletRequestUtil.getLong(request, "shopCategoryId");
			//试着获取区域id
			int areaId = HttpServletRequestUtil.getInt(request, "areaId");
			//试着获取模糊查询的名字
			String shopName = HttpServletRequestUtil.getString(request, "shopName");
			//获取组合之后的查询条件
			Shop shopCondition=compactShopCondition4Search(parentId,shopCategoryId,areaId,shopName);
			//根据查询条件和分页信息获取店铺列表，并返回总数
			ShopExecution se = shopService.getShopList(shopCondition, pageIndex, pageSize);
			modelMap.put("shopList", se.getShopList());
			modelMap.put("count", se.getCount());
			modelMap.put("success", true);
		}else{
			logger.error("页码信息为空");
			modelMap.put("success", false);
			modelMap.put("errMsg", "empty pageSize or pageIndex");
		}
		logger.info("获取指定列表下的店铺列表信息方法结束");
		return modelMap;
		
		
		
	}

	/**
	 * 
	* @Title: compactShopCondition4Search 
	* @Description: 获取组合在一起的查询店铺条件 
	* @param parentId
	* @param shopCategoryId
	* @param areaId
	* @param shopName
	* @return Shop
	* @author haohao
	* @date 2018年9月20日下午9:26:06
	 */
	private Shop compactShopCondition4Search(long parentId, long shopCategoryId, int areaId, String shopName) {
		Shop shopCondition = new Shop();
		if(parentId !=-1L){
			logger.debug("组合一级列表下的二级列表下的店铺信息条件");
			//查询某个一级ShopCategory下面的所有二级ShopCategory里面的店铺列表
			ShopCategory childCategory = new ShopCategory();
			ShopCategory parentCategory = new ShopCategory();
			parentCategory.setShopCategoryId(parentId);
			childCategory.setParent(parentCategory);
			shopCondition.setShopCategory(childCategory);
		}
		if(shopCategoryId !=-1L){
			logger.debug("组合二级店铺列表下的店铺信息条件");
			//查询某个二级ShopCategory下面店铺列表
			ShopCategory shopCategory = new ShopCategory();
			shopCategory.setShopCategoryId(shopCategoryId);
			shopCondition.setShopCategory(shopCategory);
			
		}
		if(areaId!=-1L){
			logger.debug("组合所属区域列表条件");
			Area area = new Area();
			area.setAreaId(areaId);
			shopCondition.setArea(area);
			
		}
		if(shopName!=null){
			logger.debug("组合店铺名字条件");
			shopCondition.setShopName(shopName);
		}
		//前段展示的店铺都是审核成功的店铺
		shopCondition.setEnableStatus(1);
		logger.info("返回组合好的查询店铺条件");
		return shopCondition;
	}
	
	
	
}
